<template>
  <div>
    <!-- 修改商品 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item prop="img">
        <el-upload
          class="avatar-uploader"
          name="sbb"
          :action="`${UPLOADURL}/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="ruleForm.img" :src="ruleForm.img" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="商品名称" prop="product_name">
        <el-input
          style="width: 400px"
          v-model="ruleForm.product_name"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input style="width: 400px" v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="商品规格" prop="specifications">
        <el-input
          style="width: 400px"
          v-model="ruleForm.specifications"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品描述" prop="Description">
        <el-input
          style="width: 400px"
          v-model="ruleForm.Description"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品颜色" prop="pro_color">
        <el-input style="width: 400px" v-model="ruleForm.pro_color"></el-input>
      </el-form-item>

      <el-form-item label="已售多少" prop="sales">
        <el-input style="width: 400px" v-model="ruleForm.sales"></el-input>
      </el-form-item>

      <el-form-item label="商品分类" prop="category_id">
        <el-radio-group style="width: 400px" v-model="ruleForm.category_id">
          <el-radio :value="1" label="女子服饰"></el-radio>
          <el-radio :value="2" label="男子服饰"></el-radio>
          <el-radio :value="3" label="家用智能"></el-radio>
          <el-radio :value="4" label="身体数据"></el-radio>
          <el-radio :value="5" label="运动装备"></el-radio>
          <el-radio :value="6" label="潮鞋"></el-radio>
          <el-radio :value="7" label="运动护具"></el-radio>
          <el-radio :value="8" label="健康食品"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即修改</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from "@/http";
export default {
  data() {
    return {
      ruleForm: {
        product_id: this.$route.query.product_id,
        img: "",
        product_name: "",
        price: "",
        sales: "",
        category_id: "",
        specifications: "",
        Description: "",
        pro_color: "",
      },

      rules: {
        product_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入商品价格", trigger: "blur" }],
        sales: [{ required: true, message: "必填", trigger: "blur" }],
        category_id: [
          { required: true, message: "请选择分类", trigger: "change" },
        ],
        specifications: [
          { required: true, message: "必填", trigger: "change" },
        ],
        Description: [{ required: true, message: "必填", trigger: "change" }],
        pro_color: [{ required: true, message: "必填", trigger: "change" }],
      },
    };
  },

  mounted() {
    this.queryMall();
  },

  methods: {
    // 查询信息
    queryMall() {
      let product_id = this.ruleForm.product_id;
      console.log(product_id);
      httpApi.mallApi.query({ product_id }).then((res) => {
        console.log("商品详情....", res);

        // 覆盖
        this.ruleForm = res.data.data[0];
        this.ruleForm.category_id = this.ruleForm.category_id;
        this.ruleForm.product_name = this.ruleForm.product_name;
        this.ruleForm.price = this.ruleForm.price;
        this.ruleForm.specifications = this.ruleForm.specifications;
        this.ruleForm.Description = this.ruleForm.Description;
        this.ruleForm.pro_color = this.ruleForm.pro_color;
        this.ruleForm.sales = this.ruleForm.sales;
        this.ruleForm.img = this.ruleForm.img;
      });
    },

    // 修改商品
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          httpApi.mallApi.update(this.ruleForm).then((res) => {
            console.log("修改成功", res);
            if (res.data.code == 200) {
              // 修改成功
              this.$message({ type: "success", message: "成功" });
              this.$router.push("/home/product-list");
            } else {
              this.$message({ type: "error", message: "系统异常" });
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    handleAvatarSuccess(res, file) {
      this.ruleForm.img = res.pic;
      console.log(res);
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.el-input {
  width: 200px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  display: block;
}
</style>
